package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * Created by 94958 on 2017/8/15.
 */
@Slf4j
@Component
public class ConsumeDao {

    public int heartBeatOp(JSONObject paramObject) throws SQLException {
        log.info("==================调用dao=================");

        String ip = paramObject.getString("ip");
        String no = paramObject.getString("version");
        String env = paramObject.getString("environment");
        Timestamp handletime = paramObject.getTimestamp("handletime");  //最新HANFDLETIME

        Timestamp last_handletime; //上一次HANDLETIME
        String distantime;  //上次报文间隔（秒）

        int code;
        String sql;

        //获取明细最近一次HANDLETIME
        sql = "select * from OMS_MANTIS where NO=? order by HANDLETIME DESC limit 1";
        JSONObject jo = QueryEngine.getInstance().doQueryObject(sql,new Object[]{no});

        Timestamp nowdate = new Timestamp(System.currentTimeMillis());  //创建和修改时间

        //添加明细数据
        log.info("================添加明细数据=================");
        Long OMS_MANTIS_id = Tools.getSequence("OMS_MANTIS");
        sql = "insert into OMS_MANTIS (ID, AD_CLIENT_ID, IP, NO, ENV, HANDLETIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?,?)";
        code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTIS_id, 37, ip, no, env, handletime, 893, 893, nowdate, nowdate});

        if(code <= 0){ //添加失败
            log.info("===============添加明细数据失败==============");
            return code;
        }

        //测试监控数据是否添加成功
        //sql="select * from OMS_MANTISREPORT";
        //JSONArray reports = QueryEngine.getInstance().doQueryObjectArray(sql);
        //System.out.println(reports.getJSONObject(1).getString("NO"));

        //检查是否含有某NO的监控数据
        sql="select * from OMS_MANTISREPORT where NO=? limit 1";
        JSONObject report = QueryEngine.getInstance().doQueryObject(sql,new Object[]{no});

        //更新监控数据
        if(jo != null){
            last_handletime = jo.getTimestamp("HANDLETIME");
            distantime = ( handletime.getTime() - last_handletime.getTime() )/1000 + "";

            if(report != null){
                log.info("===================更新监控数据====================");

                sql = "update OMS_MANTISREPORT set IP=?, DISTANTIME=?, MODIFIEDDATE=? where NO=?";
                code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{ip, distantime, nowdate, no});

            }else{
                log.info("===================添加监控数据(distantime有值)====================");

                Long OMS_MANTISREPORT_id = Tools.getSequence("OMS_MANTISREPORT");

                sql = "insert into OMS_MANTISREPORT (ID, AD_CLIENT_ID, IP, NO, DISTANTIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?)";
                code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTISREPORT_id, 37, ip, no, distantime, 893, 893, nowdate, nowdate});
            }

        }else{
            log.info("=================添加监控数据(distantime无值)=================");

            Long OMS_MANTISREPORT_id = Tools.getSequence("OMS_MANTISREPORT");

            sql = "insert into OMS_MANTISREPORT (ID, AD_CLIENT_ID, IP, NO, DISTANTIME, OWNERID, MODIFIERID, CREATIONDATE, MODIFIEDDATE) values(?,?,?,?,?,?,?,?,?)";
            code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{OMS_MANTISREPORT_id, 37, ip, no, 0, 893, 893, nowdate, nowdate});
        }

        return 0;
    }
}
